package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.FavouriteRecord;
import com.buaa.airticketbookingsystem.Entity.Flight;
import com.buaa.airticketbookingsystem.Service.FavouriteService;
import com.buaa.airticketbookingsystem.Service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
public class FavoriteController {

    @Autowired
    private FavouriteService favouriteService;

    @Autowired
    private FlightService flightService;

    @PostMapping("/favorite")
    public Map<String, Object> addFavoriteRecord(@RequestBody Map<String, String> remap){
        String email = remap.get("email");
        String f_id = remap.get("f_id");
        String date = remap.get("date");
        Map<String, Object> resMap = new HashMap<>();
        System.out.println(f_id);
        System.out.println(date);
        try{
            Flight f = flightService.selectFlightByID(f_id);
            favouriteService.insertFavourite(new FavouriteRecord(f_id, email, date));
            resMap.put("res", true);
            resMap.put("message", "添加收藏成功");
        } catch(Exception e){
            e.printStackTrace();
            resMap.put("res", false);
            resMap.put("message", "添加收藏失败");
        }
        return resMap;
    }
}
